package top.continew.admin.business.service.impl;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.admin.business.mapper.KnowledgePointTreeMapper;
import top.continew.admin.business.model.entity.KnowledgePointTreeDO;
import top.continew.admin.business.model.query.KnowledgePointTreeMultiSpecTypeQuery;
import top.continew.admin.business.model.query.KnowledgePointTreeQuery;
import top.continew.admin.business.model.req.KnowledgePointTreeReq;
import top.continew.admin.business.model.resp.KnowledgePointTreeDetailResp;
import top.continew.admin.business.model.resp.KnowledgePointTreeResp;
import top.continew.admin.business.service.KnowledgePointTreeService;
import top.continew.starter.extension.crud.service.BaseServiceImpl;

import java.util.List;

/**
 * 知识点树业务实现
 *
 * @author sheng_chao
 * @since 2025/03/05 10:51
 */
@Service
@RequiredArgsConstructor
public class KnowledgePointTreeServiceImpl extends BaseServiceImpl<KnowledgePointTreeMapper, KnowledgePointTreeDO, KnowledgePointTreeResp, KnowledgePointTreeDetailResp, KnowledgePointTreeQuery, KnowledgePointTreeReq> implements KnowledgePointTreeService {
    @Override
    public void beforeCreate(KnowledgePointTreeReq req) {
        if (req.getParentId() != 0) {
            KnowledgePointTreeDO knowledgePointTreeResp = this.baseMapper.selectById(req.getParentId());
            if (StrUtil.isNotEmpty(knowledgePointTreeResp.getPath())) {
                req.setPath(knowledgePointTreeResp.getPath() + "/" + req.getParentId());
            } else {
                req.setPath(String.valueOf(req.getParentId()));
            }
        } else {
            req.setPath("");
        }
    }

    @Override
    public List<KnowledgePointTreeResp> listBySpecTypeExtends(KnowledgePointTreeMultiSpecTypeQuery query, Object o) {
        return getBaseMapper().listBySpecTypeExtends(query);
    }
}